package com.ruoyi.project.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.utils.ImgUtil;
import com.ruoyi.project.domain.MonthlyImage;
import com.ruoyi.project.mapper.MonthlyImageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.MonthlyImageDetailMapper;
import com.ruoyi.project.domain.MonthlyImageDetail;
import com.ruoyi.project.service.IMonthlyImageDetailService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 月度形象图详情Service业务层处理
 * 
 * @author liboyu
 * @date 2024-06-04
 */
@Service
public class MonthlyImageDetailServiceImpl implements IMonthlyImageDetailService 
{
    @Autowired
    private MonthlyImageMapper monthlyImageMapper;

    @Autowired
    private MonthlyImageDetailMapper monthlyImageDetailMapper;

    /**
     * 查询月度形象图详情
     * 
     * @param imageDetailId 月度形象图详情主键
     * @return 月度形象图详情
     */
    @Override
    public MonthlyImageDetail selectMonthlyImageDetailByImageDetailId(Long imageDetailId)
    {
        return monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(imageDetailId);
    }

    /**
     * 查询月度形象图详情列表
     * 
     * @param monthlyImageDetail 月度形象图详情
     * @return 月度形象图详情
     */
    @Override
    public List<MonthlyImageDetail> selectMonthlyImageDetailList(MonthlyImageDetail monthlyImageDetail)
    {
        return monthlyImageDetailMapper.selectMonthlyImageDetailList(monthlyImageDetail);
    }

    /**
     * 新增月度形象图详情
     * 
     * @param monthlyImageDetail 月度形象图详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int insertMonthlyImageDetail(MonthlyImageDetail monthlyImageDetail)
    {
        // 设定时间为当前时间
        MonthlyImage monthlyImage =new MonthlyImage();
        monthlyImage.setImageId(monthlyImageDetail.getImageId());
        monthlyImage.setUpdateDate(new Date());
        monthlyImageMapper.updateMonthlyImage(monthlyImage);
//        // 设定详情上传时间为当前时间
//        monthlyImageDetail.setImageDetailDate(new Date());
        return monthlyImageDetailMapper.insertMonthlyImageDetail(monthlyImageDetail);
    }

    /**
     * 修改月度形象图详情
     * 
     * @param monthlyImageDetail 月度形象图详情
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int updateMonthlyImageDetail(MonthlyImageDetail monthlyImageDetail)
    {
        // 设定时间为当前时间
        MonthlyImage monthlyImage =new MonthlyImage();
        monthlyImage.setImageId(monthlyImageDetail.getImageId());
        monthlyImage.setUpdateDate(new Date());
        monthlyImageMapper.updateMonthlyImage(monthlyImage);
        // 设定详情上传时间为当前时间
        // monthlyImageDetail.setImageDetailDate(new Date());


        // 查询数据库中图片的路径
        String imagePathInDb =  monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(monthlyImageDetail.getImageDetailId()).getImageDetailPic();
        // 获取前端传过来的图片路径
        String imageFromView = monthlyImageDetail.getImageDetailPic();

        // 执行修改操作
        int i = monthlyImageDetailMapper.updateMonthlyImageDetail(monthlyImageDetail);

        // 删除垃圾图片
        ImgUtil.delTrushImg(i, imagePathInDb, imageFromView);

        return i;
    }

    /**
     * 批量删除月度形象图详情
     * 
     * @param imageDetailIds 需要删除的月度形象图详情主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteMonthlyImageDetailByImageDetailIds(Long[] imageDetailIds)
    {
        MonthlyImageDetail monthlyImageDetail = monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(imageDetailIds[0]);
        // 设定时间为当前时间
        MonthlyImage monthlyImage =new MonthlyImage();
        monthlyImage.setImageId(monthlyImageDetail.getImageId());
        monthlyImage.setUpdateDate(new Date());
        monthlyImageMapper.updateMonthlyImage(monthlyImage);
        // 删除硬盘中的垃圾图片
        // 查询数据库中图片的路径
        String imagePathInDb =  monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(imageDetailIds[0]).getImageDetailPic();

        //删除数据库里的图片
        ImgUtil.delDbImg(imagePathInDb);
        return monthlyImageDetailMapper.deleteMonthlyImageDetailByImageDetailIds(imageDetailIds);
    }



    /**
     * 删除月度形象图详情信息
     * 
     * @param imageDetailId 月度形象图详情主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = {Exception.class, Error.class})
    public int deleteMonthlyImageDetailByImageDetailId(Long imageDetailId)
    {
        MonthlyImageDetail monthlyImageDetail = monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(imageDetailId);
        // 设定主题的修改时间为当前时间
        MonthlyImage monthlyImage =new MonthlyImage();
        monthlyImage.setImageId(monthlyImageDetail.getImageId());
        monthlyImage.setUpdateDate(new Date());
        monthlyImageMapper.updateMonthlyImage(monthlyImage);
        // 删除硬盘中的垃圾图片
        // 查询数据库中图片的路径
        String imagePathInDb =  monthlyImageDetailMapper.selectMonthlyImageDetailByImageDetailId(imageDetailId).getImageDetailPic();

        //删除数据库里的图片
        ImgUtil.delDbImg(imagePathInDb);


        return monthlyImageDetailMapper.deleteMonthlyImageDetailByImageDetailId(imageDetailId);
    }

    @Override
    public Long checkNameUnique(MonthlyImageDetail monthlyImageDetail) {
        return monthlyImageDetailMapper.checkNameUnique(monthlyImageDetail);
    }
}
